<?php

namespace app\common\model;

use think\Db;
use think\facade\Log;

class ManageUserRole extends Common
{
    const IS_DEF_NO = 0;
    const IS_DEF_YES = 1;
    
    protected $autoWriteTimestamp = true;
    protected $updateTime = 'utime';


    protected function tableWhere($post)
    {
        $where = []; 
        if(isset($post['name']) && $post['name'] != ""){
            $where[] = ['name', 'like', '%'.$post['name'].'%'];
        }
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = 'id';
        return $result;
    }

    /**
     * 根据查询结果，格式化数据
     * @author tony
     * @param $list  array格式的collection
     * @return mixed
     */
    protected function tableFormat($list)
    {
        foreach($list as $k => $v) {
            if($v['utime']) {
                $list[$k]['utime'] = getTime($v['utime']);
            }

            if($v['is_def'] == self::IS_DEF_YES){
                $list[$k]['is_def'] = "默认";
            }else{
                $list[$k]['is_def'] = "";
            }
        }

        return $list;
    }

    public function toDel($id)
    {
        $result = [
            'status' => false,
            'data' => [],
            'msg' => ''
        ];

        $where['id'] = $id;

        // 判断当前菜单是否是使用状态，使用中无法删除
        $relModel = new ManageUserRoleRel(); 
        $rel = $relModel->where(['role_id '=>$id])->select();
        if($rel->isEmpty()){
            $mrorModel = new ManageUserRoleOperationRel();

            Db::startTrans();
            try {
                $this->where($where)->delete();
                $mrorModel->where(['manage_user_role_id'=>$id])->delete();
                Db::commit();
                $result['status'] = true;
                $result['msg'] = '删除成功';
            } catch (\Exception $e) {
                Db::rollback();
                $result['msg'] = $e->getMessage();
            }
            return $result;
        }else{
            return error_code(11072);
        }
    }

    /**
     * 返回userRoleList
     * @author tony
     * @param $post
     * @return mixed
     */
    public function getUserRoleList()
    {
        $list       = [];
        $list = $this->field('id, name')->select();

        $userRoleArray = $list->toArray();
        $userRoleList = [];
        foreach ($userRoleArray as $k => $v) {
            $userRoleList[$v['name']] = $v['id'];
        }
         
        return $userRoleList;
    }

    /**
     * 取角色的操作权限数组
     * @param $id
     * @return array|\think\Config
     */
    public function getRoleOperation($id)
    {
        $result = [
            'status' => true,
            'data'   => [],
            'msg'    => ''
        ];

        $where['id']    = $id;
        $sellerRoleInfo = $this->where($where)->find();
        if (!$sellerRoleInfo) {
            return error_code(11071);
        }
        $mrorModel = new ManageUserRoleOperationRel();
        $permList  = $mrorModel->where(['manage_user_role_id' => $id])->select();
        if (!$permList->isEmpty()) {
            $nodeList = array_column($permList->toArray(), 'manage_user_role_id', 'operation_id');
        } else {
            $nodeList = [];
        }

        $operationModel = new WxOperation();
        $result['data'] = $operationModel->menuTree($operationModel::MENU_MANAGE, $nodeList);
        return $result;
    } 
}